package httpUtils

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/go-resty/resty/v2"
)

type Do func(r *resty.Request) (*resty.Response, error)

func FormatResponse(f Do, isWeb ...bool) {
	if f != nil {
		r := resty.New().R()
		if len(isWeb) == 0 {
			r.SetHeader("User-Agent", "ios")
		}
		resp, err := f(r)
		if err != nil {
			fmt.Println(err)
		} else {
			if resp == nil {
				fmt.Println("nil")
			} else {
				var bb bytes.Buffer
				err = json.Indent(&bb, []byte(resp.String()), "", "    ")
				if err == nil {
					fmt.Println(bb.String())
				} else {
					fmt.Println(resp.String() + " : " + r.URL)
				}
			}
		}
	} else {
		fmt.Println("f is nil")
	}
}
